﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.BLL.Core.IService;
using Empires.RAL.Access;
using Empires.RAL.Pump;
using Empires.CRL.DataContract;
using Empires.CRL.Tools;
using System.IO;
using Empires.BLL.ServerProvider;

namespace Empires.BLL.Core.Implement
{
    public class CompanyBaseService : ICompanyBaseService
    {
        public int GetCompanyId(string copName)
        {
            ICompanyBaseAccess company = new CompanyBaseDataPump();
            return company.QueryCompanyId(copName);
        }

        public string GetCompanyName(int id)
        {
            ICompanyBaseAccess company = new CompanyBaseDataPump();
            return company.QueryCompanyName(id);
        }

        public string GetCompanyTempleteBasePath(int id)
        {
            ICompanyBaseAccess company = new CompanyBaseDataPump();
            return company.QueryCompanyTempleteBasePath(id);
        }
        

        public string GetCompanyTempleteBasePath(string copName)
        {
            ICompanyBaseAccess company = new CompanyBaseDataPump();
            return company.QueryCompanyTempleteBasePath(copName);
        }

        public List<DefaultDataPack> GetAuthenticationInfo(string copName)
        {
            List<DefaultDataPack> result = new List<DefaultDataPack>() { new DefaultDataPack() };
            Empires.BLL.ServerProvider.RoleCheckProvider.SetAuthInfo(ref result, copName);
            return result;
        }

        public string GetMasterName(string copname)
        {
            ICompanyBaseAccess access = new CompanyBaseDataPump();
            return access.GetMasterNameByCopName(copname);
        }


        public string GetCompanyRescourceBasePath(string copName, CompanyRescourceType resourceType)
        {
            string path = string.Empty;
            switch(resourceType)
            {
                case CompanyRescourceType.Files:
                    path = Utils.ReadAppSetting("CustomerFile");
                    break;
                case CompanyRescourceType.Image:
                    path = Utils.ReadAppSetting("CustomerImage");
                    break;
            }
            if (String.IsNullOrEmpty(path))
            {
                throw new NullReferenceException("读取配置文件基础用户资源文件模板路径失败，请检查webconfig中包含指定key的appsetting配置");
            }
            string directory = String.Format(path,copName);
            if (!Directory.Exists(directory))
            {
                CompanyHelper.CreateCompanyDirectory(copName);
            }
            return directory;
        }


        //public List<DefaultDataPack> GetCompanyMainInfo(string copname, int take)
        //{
        //    ICompanyBaseAccess access = new CompanyBaseDataPump();
        //    return access.GetCompanyMainInfo(copname, take);
        //}


        public List<DefaultDataPack> GetCompanyMainInfoItem(string copname, int bid)
        {
            ICompanyLinksAccess access = new CompanyLinksDataPump();
            return access.GetCompanyMainInfoItem(copname, bid);
        }
    }
}
